import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
  
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/student',
      component: () => import('../views/student/management.vue'),
      children: [
        {
          path: 'user',
          name: 'StudentUser',
          component: () => import('../views/student/User.vue'),
        },
        {
          path: 'Listener',
          name: 'Listener',
          component: () => import('../views/student/Listener.vue'),
        },
        {
          path: 'Face',
          name: 'Face',
          component: () => import('../views/student/Face.vue'),
        },
        {
          path: 'Job',
          name: 'Job',
          component: () => import('../views/student/Job.vue'),
        },
        {
          path: 'Interview',
          name: 'Interview',
          component: () => import('../views/student/Interview.vue'),
        },
        {
          path: 'XrtcPlayer',
          name: 'XrtcPlayer',
          component: () => import('../views/student/XrtcPlayer.vue'),
        },
        {
          path: 'InterviewResult',
          name: 'InterviewResult',
          component: () => import('../views/student/InterviewResult.vue'),
        },
        {
          path: 'Home',
          name: 'Home',
          component: () => import('../views/student/Home.vue'),
        },
        {
          path: 'SpeechEvaluation',
          name: 'SpeechEvaluation',
          component: () => import('../views/student/SpeechEvaluation.vue'),
        },
        {
          path: 'LearningPath',
          name: 'LearningPath',
          component: () => import('../views/student/LearningPath.vue'),
        },
        {
          path: 'QuestionTraining',
          name: 'QuestionTraining',
          component: () => import('../views/student/QuestionTraining.vue'),
        },

      ]
    },
    {
      path: '/admin',
      component: () => import('../views/admin/management.vue'),
      children: [
        {
          path: 'home',
          name: 'AdminHome',
          component: () => import('@/views/admin/Home.vue')
        },
        {
          path: 'user',
          name: 'AdminUser',
          component: () => import('../views/admin/User.vue'),
        },
        {
          path: 'Question',
          name: 'Question',
          component: () => import('../views/admin/Question.vue'),
        }
      ]
    },
    {
      path: '/school',
      component: () => import('../views/school/management.vue'),
      children: [
        {
          path: 'home',
          name: 'SchoolHome',
          component: () => import('@/views/school/Home.vue')
        },
        {
          path: 'user',
          name: 'SchoolUser',
          component: () => import('../views/school/User.vue'),
        },
      ]
    },
    {
      path: '/404',
      name: 'Notfound',
      meta: { title: '404 找不到页面' },
      component: () => import('../views/404.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
      name: 'notFound',
      meta: { title: '404 找不到页面' },
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
    }
    // {
    //   path: '/learning',
    //   name: 'Learning',
    //   component: () => import('../views/Learning.vue'),
    // },
    // {
    //   path: '/profile',
    //   name: 'Profile',
    //   component: () => import('../views/Profile.vue'),
    // },
    // {
    //   path: '/xunfei',
    //   name: 'Xunfei',
    //   component: () => import('../views/xunfei.vue'),
    // },
    // {
    //   path: '/chat',
    //   name: 'chat',
    //   component: () => import('../views/chat.vue'),
    // },
  ],
})
// 导航守卫 - 权限控制
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  const userRole = localStorage.getItem('role')
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 智能数字人面试系统`
  }

  // 检查是否需要认证
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
    return
  }

  // 检查角色权限
  if (to.meta.role && userRole !== to.meta.role) {
    next('/404')
    return
  }

  next()
})
export default router